package jmcnet.libcommun.communs
{
	import flash.utils.Dictionary;
	
	import jmcnet.libcommun.communs.exceptions.ErrorIllegalEnumValue;
	import jmcnet.libcommun.communs.enum.EnumBase;
	
	public class EnumSample extends EnumBase
	{
		public static var TYPE_0:EnumSample = new EnumSample("valeur0");
		public static var TYPE_1:EnumSample = new EnumSample("valeur1");
		public static var TYPE_2:EnumSample = new EnumSample("valeur2");
		
		public function EnumSample(value:String) {
			super(value);
		}
		
		public static function init():Boolean {
			addPossibleValue("TYPE_0", TYPE_0);
			addPossibleValue("TYPE_1", TYPE_1);
			addPossibleValue("TYPE_2", TYPE_2);
			return true;
		}
		
		/**********************************************************************************************************************
		 * Partie commune a tous les Enum
		 */
		
		private static var _initDone:Boolean = false;
   	    private static var _values:Dictionary = new Dictionary();
	    private static var _literals:Array = new Array();
	    private static var _names:Array = new Array();
	    private static var _valueList:Array = new Array();
	    
  		static private const res:Boolean = init();
  		
  		/**
	     * Gets de index of the value in the list of possibles values
	     * @return the index of the value or -1 if the value is not one of the possible
	     */
	    override public function get selectedIndex():int { return _literals.indexOf(value, 0); }
	    
	    /**
	     * Ajoute une valeur possible a cet énuméré 
	     */
	    public static function addPossibleValue(name:String, possibility:EnumBase):void {
	    	_values[possibility.value] = possibility;
	    	_valueList.push(possibility);
	    	_literals.push(possibility.value);
	    	_names.push(name);
	    }
	
	    public static function fromString(value:String):EnumSample {
	        var typeValue:EnumSample = _values[value];
	        if (typeValue == null)
	        {
	            throw new ErrorIllegalEnumValue("invalid value '" + value + "', possible values are: " + _literals);
	        }
	        return typeValue;
	    }
	    
	    public static function get literals():Array { return _literals; }
	    public static function get names():Array { return _names; }	    
	    public static function get values():Array  { return _valueList; }
	    public static function literalsExtended(firstChoice:String):Array { return new Array().concat(firstChoice, _literals);}
	}
}